package com.qingzhou.web.service.impl;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.qingzhou.common.utils.FileUtil;
import com.qingzhou.web.service.RegionService;
import com.qingzhou.web.service.impl.params.vo.AreaVo;
import com.qingzhou.web.service.impl.params.vo.CityVo;
import com.qingzhou.web.service.impl.params.vo.ProvinceVo;
import com.qingzhou.web.service.impl.params.vo.RegionVo;
import org.noear.solon.annotation.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class RegionServiceImpl implements RegionService {
    @Override
    public List<RegionVo> regions() {
        // 读取resources/region/provinces.json cities.json内容

        String provinceStr = IoUtil.readUtf8(this.getClass().getClassLoader().getResourceAsStream("region/provinces.json"));
        List<ProvinceVo> provinceList = JSON.parseArray(provinceStr, ProvinceVo.class);

        String cityStr = IoUtil.readUtf8(this.getClass().getClassLoader().getResourceAsStream("region/cities.json"));
        List<CityVo> cityList = JSON.parseArray(cityStr, CityVo.class);

        String areaStr = IoUtil.readUtf8(this.getClass().getClassLoader().getResourceAsStream("region/areas.json"));
        List<AreaVo> areaList = JSON.parseArray(areaStr, AreaVo.class);

        List<RegionVo> regionList = new ArrayList<>();
        Map<String,RegionVo> provinceMap = new HashMap<>();
        provinceList.forEach(item->{
            RegionVo vo = new RegionVo(item.getCode(),item.getName());
            vo.setChildren(new ArrayList<>());
            provinceMap.put(vo.getValue(),vo);
            regionList.add(vo);
        });

        Map<String,RegionVo> cityMap = new HashMap<>();
        cityList.forEach(item->{
            RegionVo city = new RegionVo(item.getCode(),item.getName());
            city.setChildren(new ArrayList<>());
            RegionVo province = provinceMap.get(item.getProvinceCode());
            province.getChildren().add(city);
            cityMap.put(item.getCode(),city);
        });

        areaList.forEach(item->{
            RegionVo area = new RegionVo(item.getCode(),item.getName());
            RegionVo city = cityMap.get(item.getCityCode());
            city.getChildren().add(area);
        });
        return regionList;
    }

    public Map<String,String> getRegionMap(){
        String provinceStr = IoUtil.readUtf8(this.getClass().getClassLoader().getResourceAsStream("region/provinces.json"));
        List<ProvinceVo> provinceList = JSON.parseArray(provinceStr, ProvinceVo.class);

        String cityStr = IoUtil.readUtf8(this.getClass().getClassLoader().getResourceAsStream("region/cities.json"));
        List<CityVo> cityList = JSON.parseArray(cityStr, CityVo.class);

        String areaStr = IoUtil.readUtf8(this.getClass().getClassLoader().getResourceAsStream("region/areas.json"));
        List<AreaVo> areaList = JSON.parseArray(areaStr, AreaVo.class);

        Map<String,String> regionMap = new HashMap<>();
        provinceList.forEach(item->{
            regionMap.put(item.getCode(),item.getName());
        });
        cityList.forEach(item->{
            regionMap.put(item.getCode(),item.getName());
        });
        areaList.forEach(item->{
            regionMap.put(item.getCode(),item.getName());
        });

        return regionMap;
    }
}
